﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace TouchUI.System.Mobile.Mvc
{

  /// <summary>
  /// Extensions to the System.Drawing.Graphics class
  /// </summary>
  public static class GraphicsExtensions
  {

    /// <summary>
    /// Measure how big a string would be using the given font and bounds.
    /// </summary>
    /// <param name="g"></param>
    /// <param name="text"></param>
    /// <param name="font"></param>
    /// <param name="rect"></param>
    /// <returns></returns>
    public static Size MeasureString(this Graphics g, string text, Font font, RectangleF rect)
    {
      // We have to use Win32 classes
      Win32Helper.RECT bounds = new Win32Helper.RECT();
      bounds.left = (int)rect.Left;
      bounds.right = (int)rect.Right;
      bounds.bottom = (int)rect.Bottom;
      bounds.right = (int)rect.Right;
      
      IntPtr hFont = font.ToHfont();
      IntPtr hdc = g.GetHdc();
      IntPtr originalObject = Win32Helper.SelectObject(hdc, hFont);
      int flags = Win32Helper.DT_CALCRECT | Win32Helper.DT_WORDBREAK;
      Win32Helper.DrawText(hdc, text, text.Length, ref bounds, flags);
      Win32Helper.SelectObject(hdc, originalObject);
      g.ReleaseHdc(hdc);
      return new Size(bounds.right - bounds.left, bounds.bottom - bounds.top);
    }

    /// <summary>
    /// Draw a Horizontal gradient using the given colors
    /// </summary>
    /// <param name="g"></param>
    /// <param name="rc"></param>
    /// <param name="startColor"></param>
    /// <param name="endColor"></param>
    /// <returns></returns>
    public static bool FillGradientH(this Graphics g, Rectangle rc, Color startColor, Color endColor)
    {
      return g.FillGradient(rc, startColor, endColor, Win32Helper.GRADIENT_FILL_RECT_H);
    }

    /// <summary>
    /// Draw a Vertical gradient using the given colors
    /// </summary>
    /// <param name="g"></param>
    /// <param name="rc"></param>
    /// <param name="startColor"></param>
    /// <param name="endColor"></param>
    /// <returns></returns>
    public static bool FillGradientV(this Graphics g, Rectangle rc, Color startColor, Color endColor)
    {
      return g.FillGradient(rc, startColor, endColor, Win32Helper.GRADIENT_FILL_RECT_V);
    }

    /// <summary>
    /// Fill a given gradient
    /// </summary>
    /// <param name="g"></param>
    /// <param name="rc"></param>
    /// <param name="startColor"></param>
    /// <param name="endColor"></param>
    /// <param name="fillDir"></param>
    /// <returns></returns>
    public static bool FillGradient(this Graphics g, Rectangle rc, Color startColor, Color endColor, int fillDir)
    {

      // Initialize the data to be used in the call to GradientFill.
      Win32Helper.TRIVERTEX[] tva = new Win32Helper.TRIVERTEX[2];
      tva[0] = new Win32Helper.TRIVERTEX(rc.X, rc.Y, startColor);
      tva[1] = new Win32Helper.TRIVERTEX(rc.Right, rc.Bottom, endColor);
      Win32Helper.GRADIENT_RECT[] gra = new Win32Helper.GRADIENT_RECT[] { new Win32Helper.GRADIENT_RECT(0, 1) };

      // Get the hDC from the Graphics object.
      IntPtr hdc = g.GetHdc();

      // PInvoke to GradientFill.
      bool b;

      b = Win32Helper.GradientFill(
              hdc,
              tva,
              (uint)tva.Length,
              gra,
              (uint)gra.Length,
              (uint)fillDir);


      // Release the hDC from the Graphics object.
      g.ReleaseHdc(hdc);

      return b;
    }

    /// <summary>
    /// Draw a rounded rectangle
    /// </summary>
    /// <param name="g"></param>
    /// <param name="p"></param>
    /// <param name="backColor"></param>
    /// <param name="rc"></param>
    /// <param name="size"></param>
    public static void DrawRoundedRectangle(this Graphics g, Pen p, Color backColor, Rectangle rc, Size size)
    {
      Point[] points = new Point[8];

      //prepare points for poligon
      points[0].X = rc.Left + size.Width / 2;
      points[0].Y = rc.Top + 1;
      points[1].X = rc.Right - size.Width / 2;
      points[1].Y = rc.Top + 1;

      points[2].X = rc.Right;
      points[2].Y = rc.Top + size.Height / 2;
      points[3].X = rc.Right;
      points[3].Y = rc.Bottom - size.Height / 2;

      points[4].X = rc.Right - size.Width / 2;
      points[4].Y = rc.Bottom;
      points[5].X = rc.Left + size.Width / 2;
      points[5].Y = rc.Bottom;

      points[6].X = rc.Left + 1;
      points[6].Y = rc.Bottom - size.Height / 2;
      points[7].X = rc.Left + 1;
      points[7].Y = rc.Top + size.Height / 2;

      //prepare brush for background
      Brush fillBrush = new SolidBrush(backColor);
      //draw side lines and circles in the corners
      g.DrawLine(p, rc.Left + size.Width / 2, rc.Top,
       rc.Right - size.Width / 2, rc.Top);
      g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Top,
       size.Width, size.Height);
      g.DrawEllipse(p, rc.Right - size.Width, rc.Top,
       size.Width, size.Height);

      g.DrawLine(p, rc.Right, rc.Top + size.Height / 2,
       rc.Right, rc.Bottom - size.Height / 2);
      g.FillEllipse(fillBrush, rc.Right - size.Width, rc.Bottom - size.Height,
       size.Width, size.Height);
      g.DrawEllipse(p, rc.Right - size.Width, rc.Bottom - size.Height,
       size.Width, size.Height);

      g.DrawLine(p, rc.Right - size.Width / 2, rc.Bottom,
       rc.Left + size.Width / 2, rc.Bottom);
      g.FillEllipse(fillBrush, rc.Left, rc.Bottom - size.Height,
       size.Width, size.Height);
      g.DrawEllipse(p, rc.Left, rc.Bottom - size.Height,
       size.Width, size.Height);

      g.DrawLine(p, rc.Left, rc.Bottom - size.Height / 2,
       rc.Left, rc.Top + size.Height / 2);
      g.FillEllipse(fillBrush, rc.Left, rc.Top,
       size.Width, size.Height);
      g.DrawEllipse(p, rc.Left, rc.Top,
       size.Width, size.Height);


      //fill the background and remove the internal arcs  
      g.FillPolygon(fillBrush, points);
      //dispose the brush
      fillBrush.Dispose();


    }
  }
}
